package mediaCoreLib.core
{
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.media.Sound;
	import flash.net.URLRequest;

	public class PlaylistObject extends EventDispatcher
	{
		private var _url:String;
		
		public function get url ():String
		{
			return _url;
		}
		
		public function set url (value:String):void
		{
			if (_url != value)
			{
				_url = value;
				var request:URLRequest = new URLRequest(_url);
				
				//if old sound, clear its listeners that may have not completed
				if (_sound)
				{
					_sound.removeEventListener(Event.COMPLETE, onComplete);
					_sound.removeEventListener(IOErrorEvent.IO_ERROR, onIOError);
				}
				
				_sound = new Sound();
				
				_sound.addEventListener(Event.COMPLETE, onComplete);
				_sound.addEventListener(IOErrorEvent.IO_ERROR, onIOError);
				_sound.load(request);
			}
		}
		
		private var _loaded:Boolean;
		
		[Bindable("isLoadedChanged")]
		public function get isLoaded ():Boolean
		{
			return _loaded;
		}
		
		private function onComplete (evt:Event):void
		{
			_sound.removeEventListener(IOErrorEvent.IO_ERROR, onIOError);
			_sound.removeEventListener(Event.COMPLETE, onComplete);
			
			_loaded = true;
			dispatchEvent(new Event("isLoadedChanged"));
		}
		
		private function onIOError (evt:IOErrorEvent):void
		{
			_sound.removeEventListener(IOErrorEvent.IO_ERROR, onIOError);
			_sound.removeEventListener(Event.COMPLETE, onComplete);
		}
		
		private var _sound:Sound;
		
		public function get sound ():Sound
		{
			return _sound;
		}
	}
}